import com.google.common.collect.Maps;
import org.apache.logging.log4j.core.util.JsonUtils;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author KenLiang
 * @description:
 * @date Created in 2:57 PM 2020/11/9.
 */

public class CodeTest {

    /**
     *
     * 字符串替换
     * @author KenLiang
     * @date 2020/11/9 7:24 PM
     */
    @Test
    public void strReplace() {
        String str = "hello are you";
        System.out.println(str.replace(" ", "%20"));
        char[] chars = str.toCharArray();
        char space = ' ';
        int count = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == space) {
                count++;
            }
        }
        char[] charsNew = new char[chars.length + count * 2];
        for (int i = 0, j = 0; i < chars.length; i++) {
            if (chars[i] == space) {
                charsNew[j] = '%';
                charsNew[j + 1] = '2';
                charsNew[j + 2] = '0';
                j += 3;
            } else {
                charsNew[j] = chars[i];
                j++;
            }
        }
        System.out.println(String.copyValueOf(chars));
        System.out.println(String.copyValueOf(charsNew));

    }

    /**
     *
     * 链表加法
     * @author KenLiang
     * @date 2020/11/9 7:24 PM
     */
    @Test
    public void numAdd() {
        LinkedNum linkedNum1 = new LinkedNum(364);
        LinkedNum linkedNum2 = new LinkedNum(7089);
        LinkedNum linkedNum = this.linkedNumAdd(linkedNum1, linkedNum2);
        LinkedNum.Node next = linkedNum.head;
        while (next !=null){
            System.out.println(next.num);
            next=next.next;
        }
    }

    private static class LinkedNum {
        private Node head;
        private Node tail;
        private int numAll;

        public LinkedNum() {
        }

        public LinkedNum(int numAll) {
            this.numAll = numAll;
            int result = 0;
            int resultDivide = 0;
            int divideNum = 1;
            resultDivide = (numAll / divideNum);
            do {
                result = resultDivide % 10;
                Node node = new Node(result);
                if (head == null) {
                    head = node;
                    tail = node;
                } else {
                    tail.next = node;
                    tail = node;
                }
                divideNum *= 10;
                resultDivide = (numAll / divideNum);

            } while (resultDivide != 0);
        }

        public Node addNode(Node node) {
            if (tail == null) {
                head = node;
            } else {
                tail.next = node;
            }
            tail = node;
            return node;
        }

        private static class Node {
            private int num;
            private Node next;

            public Node(int num) {
                this.num = num;
            }
        }

    }

    private LinkedNum linkedNumAdd(LinkedNum linkedNum1, LinkedNum linkedNum2) {
        LinkedNum.Node node1 = linkedNum1.head;
        LinkedNum.Node node2 = linkedNum2.head;
        int nodeNum1 = 0;
        int nodeNum2 = 0;
        boolean lastAdd = false;
        int sum = 0;
        LinkedNum linkedNum = new LinkedNum();
        do {

            nodeNum1 = node1 == null ? 0 : node1.num;
            nodeNum2 = node2 == null ? 0 : node2.num;
            sum = nodeNum1 + nodeNum2;

            linkedNum.addNode(new LinkedNum.Node(lastAdd ? sum % 10 + 1 : sum % 10));

            if (sum > 9) {
                lastAdd = true;
            }else{
                lastAdd=false;
            }
            if (node1 != null) {
                node1 = node1.next;
            }
            if (node2 != null) {
                node2 = node2.next;
            }
        } while (node1 != null || node2 != null);
        if (lastAdd) {
            linkedNum.addNode(new LinkedNum.Node(1));
        }
        return linkedNum;
    }

    /**
     *
     * 小孩上台阶的可能性
     * @author KenLiang
     * @date 2020/11/9 7:24 PM
     */
    @Test
    public void upstairs() {
        System.out.println(getNumUpstairs(30, new HashMap<>()));
    }

    private int getNumUpstairs(int n, Map<Integer, Integer> map) {
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        if (n == 3) {
            return 3;
        }
        if (map.get(n) != null) {
            return map.get(n);
        }
        return getNumUpstairs(n - 1, map) + getNumUpstairs(n - 2, map) + getNumUpstairs(n - 3, map);
    }
}

